﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Data.OleDb;
using ESOL.Business.Sec;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Sec
{
    public partial class frmMaintainImportObjects : BaseForm.frmBaseDialogyMaintainForm
    {
        public frmMaintainImportObjects()
        {
            InitializeComponent();
        }

        private void BindTreeRoleMenuObject()
        {
            DataTable dt = SecMenuBs.GetAllDsMenus();
            DataTable dtCopy = dt.Copy();
            foreach (DataRow dr in dt.Rows)
            {
                if (!dr["ObjectType"].Equals("20"))
                {
                    continue;
                }
                List<SecObjectEntity> list = SecObjectBs.GetSecObjectByMenuID(dr["Id"].ToString());
                if (list == null || list.Count == 0)
                {
                    continue;
                }
                foreach (SecObjectEntity m in list)
                {
                    DataRow drCopy = dtCopy.NewRow();
                    drCopy["Id"] = m.ObjectId;
                    drCopy["ParentId"] = dr["Id"].ToString();
                    drCopy["ObjectType"] = "21";//按钮对象
                    drCopy["Name"] = m.ObjectName;
                    dtCopy.Rows.Add(drCopy);
                }
            }
            treeRoleMenuObject.DataSource = dtCopy;
            treeRoleMenuObject.ExpandAll();
        }

        private bool CheckFileExist()
        {
            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                return false;
            }
            return File.Exists(txtFilePath.Text);
        }

        private DataTable GetDataTableFromExcel()
        {
            string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + txtFilePath.Text + ";Extended Properties=Excel 8.0";
            OleDbConnection conn = new OleDbConnection(strCon);
            string tableName = "sheet1$";
            conn.Open();
            DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
            if (dt != null && dt.Rows.Count > 0)
            {
                tableName = dt.Rows[0]["TABLE_NAME"].ToString();
            }
            string sql = "select * from [" + tableName + "]";
            OleDbDataAdapter cmd = new OleDbDataAdapter(sql, strCon);
            DataSet ds = new DataSet();
            cmd.Fill(ds, "FormObjects");
            conn.Close();
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }

        private void txtFilePath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Excel文件|*.xls";
            fd.Multiselect = false;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                txtFilePath.Text = fd.FileName;
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            try
            {
                if (CheckFileExist() == false)
                {
                    MessageBox.Show("此文件不存在，请重新选择！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DataTable dt = GetDataTableFromExcel();
                if (dt == null || dt.Rows.Count == 0)
                {
                    MessageBox.Show("此文件中没有数据，请重新选择！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                //bool successful = HrAttendanceRecordBs.ImportAttendance(dt);
                pbarProgress.Properties.Minimum = 0;
                pbarProgress.Properties.Maximum = dt.Rows.Count;
                pbarProgress.Properties.Step = 1;
                pbarProgress.Properties.ProgressViewStyle= DevExpress.XtraEditors.Controls.ProgressViewStyle.Solid;
                bool successful = true;
                foreach (DataRow dr in dt.Rows)
                {
                    successful = SecObjectBs.ImportObjects(dr);
                    if (!successful)
                    {
                        break;
                    }
                    pbarProgress.PerformStep();
                }
                if (successful)
                {
                    MessageBox.Show("导入成功，共导入" + dt.Rows.Count.ToString() + "条记录！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //BindTree();
                }
                else
                {
                    MessageBox.Show("导入失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void frmMaintainImportObjects_Load(object sender, EventArgs e)
        {
            BindTreeRoleMenuObject();
        }
    }
}
